package com.chaochis.springboottestdemo.model.serviceModel;

import com.auth0.jwt.JWT;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

@Data
@Accessors(chain = true)
public class JwtToken {
  private Date createDate;
  private Date expireDate;
  private String userName;
  private String token;
  public static JwtToken build(String jwtTokenString) {
    var decodeJwt = JWT.decode(jwtTokenString);
    var jwtToken = new JwtToken();
    jwtToken.setCreateDate(decodeJwt.getIssuedAt());
    jwtToken.setExpireDate(decodeJwt.getExpiresAt());
    jwtToken.setUserName(decodeJwt.getClaim("userName").asString());
    jwtToken.setToken(jwtTokenString);
    return jwtToken;
  }
}
